<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateWaimaiSecureCardsTable extends Migration
{
    private string $name;

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('shopId')->default(0);
            $table->json('cardDetailList')->nullable()->comment('安心卡人员信息');
            $table->string('tips', 512)->default('')->comment('小贴士');
            $table->unsignedInteger('date')->default(0)->comment('安心卡日期Ymd');
            $table->unsignedInteger('createdAt')->default(0);

            $table->index(['shopId', 'date'], 'idx_shopId_date');

            $table->comment   = '外卖安心卡信息表' . $this->name;
            $table->collation = 'utf8mb4_general_ci';
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->create('waimai_secure_cards' . $this->name, $cFunc);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->dropIfExists('waimai_secure_cards' . $this->name);
        }
    }
}
